﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Com.Toook.Common;
using System.Text;
using System.Data.OleDb;
using Com.Toook.DAL;

public partial class article_list : BaseShowPage
{
    private int pageSize = 45;
    protected string ArticleListHtml = "";
    protected string PageHtml = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }
    private void InitData()
    {
        int type, page;
        DataRow row = base.GettListData(RecommendType.Article, RecommendId.Article, out type, out page);

        Master.SetTitleKeyDesc(row);

        int total = PageData.GetTotalCount(type);
        page = PageData.GetValidPageNumber(page, total, pageSize);
        PageHtml = PageData.GetPageHtml(total, pageSize, page, "list.aspx?type=" + type + "&page=pageN");
        string typePageIdConditon;
        if (type == (int)RecommendId.Article)
        {
            typePageIdConditon = " id>=" + ((page - 1) * pageSize + 1).ToString() + " and id<" + (page * pageSize + 1).ToString();
        }
        else
        {
            typePageIdConditon = PageData.GetPageStartEndCondition(type, (page - 1) * pageSize + 1, pageSize);
        }
        string sql = "select id, title, typePageId from PublishArticle where " + typePageIdConditon; // + " order by id desc";
        StringBuilder sbHtml = new StringBuilder(8192);
        OleDbConnection conn = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataReader reader = null;

        try
        {
            bool isFirst = true;
            conn.Open();
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (isFirst)
                {
                    isFirst = false;
                    sbHtml.Append("<ul>");
                }               
                sbHtml.Append("<li><a href=\"content.aspx?id=").Append(reader["typePageId"].ToString());
                sbHtml.Append("\" target=\"_blank\">");
                sbHtml.Append(reader["title"].ToString()).Append("</a></li>");
            }
            if (!isFirst)//isFirst = false;说明已经读取到数据，加上结束标签
            {
                sbHtml.Append("</ul>");
            }
        }
        finally
        {
            AccessHelper.CloseDbResource(conn, reader);
        }
        ArticleListHtml = sbHtml.ToString();
    }
    
}
